<template>
  <div>
    <div>
      <el-row style="margin-bottom:10px">
        <el-col :span="8" >
          <h4 style="margin:0"> 
            <span>搜索管理</span>
            <el-button  type="primary" class="addBtn" v-if="userRole.KW_301001" @click="addFun">添加版本</el-button>
          </h4>
        </el-col>
      </el-row>
      
      <el-table :data="tableData" border style="width: 100%" >
        <el-table-column prop="name" label="序号" width="60">
          <template slot-scope="scope">{{scope.$index+1}}</template>
        </el-table-column>
        
        <el-table-column prop="version" label="版本名称"> </el-table-column>      
        <el-table-column prop="version_code" label="版本"> </el-table-column>      
        <el-table-column prop="device_type" label="设备类型">
          <template slot-scope="scope" >
            <span v-if="scope.row.device_type ==1">ios</span>
            <span v-if="scope.row.device_type ==2">安卓</span>
          </template>
        </el-table-column>      
        <el-table-column prop="link" label="下载链接"></el-table-column>      
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope" >
            <span v-if="scope.row.status ==1">已发布</span>
            <span v-else>未发布</span>
          </template>    
        </el-table-column>      
        <el-table-column prop="force_update" label="是否强制">
          <template slot-scope="scope" >
            <span v-if="scope.row.force_update ==1">强制</span>
            <span v-else>不强制</span>
          </template>  
        </el-table-column>      
        <el-table-column prop="is_show" label="是否弹窗">
          <template slot-scope="scope" >
            <span v-if="scope.row.is_show ==1">弹窗</span>
            <span v-else>不弹窗</span>
          </template>    
        </el-table-column>      
        <el-table-column prop="description" label="描述"></el-table-column>      
          
        <el-table-column label="操作">
          <template slot-scope="scope" >
            <div>
              <el-button class="addBtn" @click="editFun(scope.row)"> 编辑</el-button>
              <!-- <el-button type="danger"  class="addBtn" @click="deletData(scope.row.id)" v-if="userRole.KW_301003"> 删除</el-button> -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="title" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">
        <el-form :model="ruleForm2" label-width="80px" :rules="rules2" ref="ruleForm2"  class="demo-ruleForm">
          <el-form-item label="选择文件" prop="keyword">
            
            <el-upload
              class="upload-demo"
              :action="action"
              :before-upload="beforeAvatarUpload"
              :on-remove="handleRemoveLogo"
              :on-success="handleOkLogo"
              :data="postData"
              :show-file-list="true"
              >
               <el-button size="small" type="primary">点击上传</el-button>
               <div v-show="ruleForm2.link"> <span style="color:#84b940">返回版本url: </span> {{ruleForm2.link}}</div>
            </el-upload>
          </el-form-item>
          <el-form-item label="版本名称" prop="version">
            <el-input v-model="ruleForm2.version"></el-input>
          </el-form-item>

          <el-form-item label="版本号" prop="version_code">
            <el-input v-model="ruleForm2.version_code"></el-input>
          </el-form-item>
         
          <el-form-item label="版本描述" prop="keyword">
            <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="ruleForm2.description"> </el-input>

          </el-form-item>
          <el-form-item label="设备类型" prop="keyword">
            <el-select v-model="ruleForm2.device_type" placeholder="请选择">
              <el-option v-for="item in device_types" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="强制更新" prop="keyword">
            <el-select v-model="ruleForm2.updata" placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否弹窗" prop="keyword">
            <el-select v-model="ruleForm2.is_show" placeholder="请选择">
              <el-option v-for="item in shows" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发布状态" prop="keyword">
            <el-select v-model="ruleForm2.status" placeholder="请选择">
              <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button  @click="handleClose" style="margin-right:80px">取 消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm2')">确 定</el-button>
        </span>
      </el-dialog>
     
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
          
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import Http from '@/Http/Http'
  import qnUrl from '@/Http/qiniuUrl'

  export default {
    data() {
      var version_code = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入版本'));
        } else {
          callback();
        }
      };
      var version = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入版本名'));
        } else {
          callback();
        }
      };

      return {
        action:qnUrl,//七牛的上传地址
        userRole:JSON.parse(sessionStorage.getItem('userSolt')),
        currentPage4: 1,//分野  
        total:0,      
        dialogVisible:false,
        tableData: [],
        page:1,
        editId:'',
        title:'添加版本',
        tokenimgUrl:'',
        pageSize:10,
        postData:{
          token:'',
          key:'',
        },
        device_types:[{value:1, label:'ios'},{value:2, label:'安卓'}],
        options:[{value: 1, label:'强制更新'},{ value: 0, label:'不强制'}],
        shows:[{value: 1, label:'弹窗'},{ value: 0, label:'不弹窗'}],
        status:[{value: 1, label:'发布'},{ value: 0, label:'不发布'}],
        ruleForm2: {
          device_type:2, //设备类型
          status:1,
          keyword: '',
          updata:0,
          link:'',
          description:'',
          version:'',
          is_show:0,
          version_code:'',
        },
        rules2: {
          version_code: [
            { validator: version_code, trigger: 'blur' }
          ],
          version: [
            { validator: version, trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getlist()
    },
    methods: {
      getlist() {
        const parm ={
          "pageIndex":this.page,
          "pageSize":this.pageSize,
        };
        
        Http.getVersionlList(parm)
        .then( res => {
          this.tableData = res.data.data.rows;
          this.total = res.data.data.total;
           this.dialogVisible = false;
          
        })
        .catch( error => {
          
        });
        Http.QiniuToken({})
        .then( res => {
          
          this.postData.token = res.data.data.uptoken;
          this.tokenimgUrl = res.data.data.domain
        })
        .catch( error => {
          
        });
      },  
      handleRemoveLogo(file, fileList) {
      
      },
      beforeAvatarUpload(file) {
        const time = Math.round(new Date() / 1000)
        this.ruleForm2.link='';
        this.postData.key = `${time}${file.name}`;
        
      },
      handleOkLogo(responed) {
        this.ruleForm2.link = `http://${this.tokenimgUrl}/${responed.key}`;
      },
      addFun(){
        this.dialogVisible = true;
        this.title = '添加版本';
      },
      editFun(val) {
        this.title = '修改版本';
        this.editId = val.id;
        this.ruleForm2.link = val.link;
        this.ruleForm2.device_type = val.device_type;
        this.ruleForm2.version = val.version;
        this.ruleForm2.status = val.status;
        this.ruleForm2.description = val.description;
        this.ruleForm2.is_show = val.is_show;
        this.ruleForm2.force_update = val.force_update;
        this.dialogVisible = true;
        this.ruleForm2.version_code = val.version_code;
      },
      deletData(row) {
        
        this.$confirm(`确认删除此关键字吗？`)
        .then(_ => {
          Http.deleteKeyword(row)
          .then( res => {
            
            if( res.data.code == 200 ){
              this.$message.success('删除成功')
              this.getlist()         
            } else if(res.data.code == -1003) {
              this.$message.error(`请求失败原因：${res.data.message}`)
              this.$router.push('/')
            }
          })
          .catch( error => {
            this.$message.error('服务器请求失败')
          })
        })
        .catch(_ => {
          //取消部分
          
        });
      },
     
      handleClose(done) {
        this.ruleForm2.link ='';
        this.ruleForm2.version = '';
        this.ruleForm2.version_code = '';
        this.ruleForm2.description = '';
        this.dialogVisible = false
      },
      
       //分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.getlist()
        
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getlist()
        
      },
     
      submitForm(formName) {
        if (!this.ruleForm2.link) { this.$message.error('请上传相关包文件'); return }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const obj = {
              device_type:this.ruleForm2.device_type,
              version: this.ruleForm2.version,
              link : this.ruleForm2.link,
              status : this.ruleForm2.status,
              description : this.ruleForm2.description,
              is_show:this.ruleForm2.is_show,
              force_update : this.ruleForm2.updata,
              version_code : this.ruleForm2.version_code
            };
            this.editId ? this.editVersionFun(obj):this.addVersionFun(obj);
             this.editId ='';
             this.ruleForm2.keyword ='';
          } else {
            return false;
          }
        });
      },
      addVersionFun(obj){
        
        Http.addVersion(obj)
        .then( res => {
          
          if( res.data.code == 200 ) { 
            this.$message.success('版本添加成功')
            
            this.getlist()    
          } else if (res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            setTimeout(()=>{
              this.$router.push('/')
            },2000)
          } else {
            this.$message.error('版本添加失败')              
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
          
        })
      },
      editVersionFun(obj){
        obj.id = this.editId;
        Http.editVersion(obj)
        .then( res => {
          
          if( res.data.code == 200 ) { 
            this.$message.success('关键字修改成功')
            
            this.getlist()    
          } else if (res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            setTimeout(()=>{
              this.$router.push('/')
            },2000)
          } else {
            this.$message.error('关键字修改失败')              
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
          
        })
      },
      
    }
  }
</script>